package com.rede.didiok.user.modulelist.rank.restapi;

import java.util.List;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.rede.didiok.common.constant.global.Constants;
import com.rede.didiok.common.dto.BusinessIdListDto;
import com.rede.didiok.common.dto.rank.RankMemberDto;
import com.rede.didiok.common.dto.user.GeneralEditDto;
import com.rede.didiok.common.enums.ContributeSourceEnum;
import com.rede.didiok.common.exception.ThrowableUtils;
import com.rede.didiok.common.utils.R;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.validator.group.Delete;
import com.rede.didiok.common.validator.group.GetList;
import com.rede.didiok.user.modulelist.rank.entity.RankMemberEntity;
import com.rede.didiok.user.modulelist.rank.service.RankMemberService;
import com.rede.didiok.user.modulelist.rank.vo.RankMemberSaveVo;


/**
 * 榜单成员
 *
 * @author Rede
 * @email didiok@foxmail.com
 * @date 2023-07-20 22:09:24
 */
@RestController
@RequestMapping("rankMember")
public class RankMemberRestApi {
    @Autowired
    private RankMemberService rankMemberService;

    /**
     * 获取文件的信息接口
     *
     * @return
     */
    @GetMapping("/getTotalRankMemberCount")
    String getTotalRankMemberCount() {
        Integer count = rankMemberService.getRankMemberCount();
        return ResultUtil.successWithData(count);

    }

    /**
     * 发布榜单成员
     *
     * @param memberUid
     * @return
     */
    @PostMapping("/{memberUid}/publish")
    public R memberPublish(@PathVariable("memberId") String memberUid) {
        rankMemberService.memberPublish(memberUid);
        return R.ok();
    }

    /**
     * 保存并发布榜单成员
     */
    @RequestMapping("/save")
    public R saveAndPublish(@RequestBody RankMemberSaveVo vo) {

        rankMemberService.saveAndPublish(vo);

        return R.ok();
    }

    /**
     * 修改榜单成员content
     *
     * @param generalEditDto
     * @return
     */
    @RequestMapping(value = "/content/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = "application/json;charset=UTF-8")
    public R updateRankMemberContent(@RequestBody GeneralEditDto generalEditDto) {

        return R.ok().setData(rankMemberService.updateRankMemberContent(generalEditDto));
    }

    /**
     * 根据 fileUid 更新对应的所有榜单成员，将其中的fileUid全都更换
     *
     * @param fileUid
     * @return
     */
    @PostMapping(value = "/picture/update")
    R updateRankMemberPictureByPictureUid(@RequestParam("fileUid") String fileUid) {
        return R.ok().setData(rankMemberService.updateRankMemberPictureByPictureUid(fileUid));
    }

    /**
     * 按照推荐等级查询榜单成员数量
     *
     * @param level
     * @return
     */
    @RequestMapping(value = "/countByLevel", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = "application/json;charset=UTF-8")
    R countRankMemberByLevel(@RequestParam("level") Integer level) {
        return R.ok().setData(rankMemberService.countRankMemberByLevel(level));
    }

    /**
     * 保存、并发布新增领域事件
     *
     * @param rankMemberDto
     * @return
     */
    @PostMapping("/addByAdmin")
    R addRankMemberByAdmin(@RequestBody RankMemberDto rankMemberDto) {

        return R.ok().setData(rankMemberService.addRankMemberByAdmin(rankMemberDto));
    }


    @ApiOperation(value = "获取榜单成员列表", notes = "获取榜单成员列表", response = String.class)
    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody RankMemberDto rankMemberDto, BindingResult result) {

        // 是否管理端查看
        rankMemberDto.setIsBack(Constants.NUM_ONE);
        ThrowableUtils.checkParamArgument(result);
        return ResultUtil.successWithData(rankMemberService.getRankMemberPageList(rankMemberDto));
    }

    /**
     * 获取榜单成员列表
     * @param rankMemberDto
     * @return
     */
    @PostMapping(value = "/getPageList")
    R getRankMemberPageList(@RequestBody RankMemberDto rankMemberDto){
        return R.ok().setData(rankMemberService.getPageListWithGradeAndRankAndTag(rankMemberDto));
    }
    /**
     * 本地榜单成员上传
     * @param fileUid
     * @return
     */
    @PostMapping("/uploadLocalRankMember")
    public String uploadLocalRankMember(@RequestBody List<RankMemberDto> list){
        return rankMemberService.uploadLocalRankMember(list);
    }

    /**
     * 编辑榜单成员
     * @param rankMemberDto
     * @return
     */
    @PostMapping(value = "/editByAdmin")
    String editRankMemberByAdmin(@RequestBody RankMemberDto rankMemberDto){
        return rankMemberService.editRankMemberByAdmin(rankMemberDto);
    }

    /**
     * 发布/下架榜单成员
     * @param fileUid
     * @return
     */
    @PostMapping("/publishByAdmin")
    String publishByAdmin(@RequestBody RankMemberDto rankMemberDto){
        return rankMemberService.publishByAdmin(rankMemberDto);
    }

    /**
     * 发布/推荐榜单成员排序调整
     * @param fileUid
     * @return
     */
    @PostMapping("/editBatch")
    String editBatch(@RequestBody List<RankMemberDto> list){
        return rankMemberService.editBatch(list);
    }

    @ApiOperation(value = "删除榜单成员", notes = "删除榜单成员", response = String.class)
    @PostMapping("/deleteByAdmin")
    public String deleteByAdmin(@Validated({Delete.class}) @RequestBody RankMemberDto rankMemberDto, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        rankMemberDto.setRankMemberSource(ContributeSourceEnum.ADMIN_PUBLISH);
        return rankMemberService.deleteRankMemberByAdmin(rankMemberDto);
    }


    @ApiOperation(value = "删除选中榜单成员", notes = "删除选中榜单成员", response = String.class)
    @PostMapping("/deleteBatch")
    public String deleteBatch(@RequestBody List<RankMemberDto> list) {
        return rankMemberService.deleteBatchRankMember(list);
    }

    @ApiOperation(value = "审核榜单成员", notes = "审核榜单成员", response = String.class)
    @PostMapping("/audit")
    public String audit(@RequestBody RankMemberDto rankMemberDto) {
        return rankMemberService.auditRankMember(rankMemberDto);
    }

    @ApiOperation(value = "榜单成员简介刷新", notes = "榜单成员简介刷数", response = String.class)
    @PostMapping("/flushRankMemberSummary")
    public String flushRankMemberSummary() {
        return rankMemberService.flushRankMemberSummary();
    }

    @ApiOperation(value = "批量审核榜单成员", notes = "批量审核榜单成员", response = String.class)
    @PostMapping("/batchAuditRankMember")
    public String batchAuditBlog(@RequestBody List<String> uidList, @RequestParam("nickname") String nickname, @RequestParam("auditStatus") String auditStatus, @RequestParam("rejectReason") String rejectReason) {
        return rankMemberService.batchAuditRankMember(uidList, nickname, auditStatus, rejectReason);
    }

    /**
     * 根据uid列表查询榜单成员
     * @param fileUid
     * @return
     */
    @GetMapping("/listByUidList")
    R listRankMemberByUidList(@RequestParam("memberUids") List<String> memberUids){
        return R.ok().setData(rankMemberService.listByIds(memberUids));
    }

    /**
     * 根据Oid列表查询榜单成员
     * @param fileUid
     * @return
     */
    @GetMapping("/listByOidList")
    R listRankMemberByOidList(@RequestParam("memberOids") List<String> memberOids){
        return R.ok().setData(rankMemberService.listByOids(memberOids));
    }

    /**
     * 榜单成员下架操作
     * @param fileUid
     * @return
     */
    @PostMapping("/revocation")
    R revocationRankMember(@RequestParam("uid") String uid){
        return R.ok().setData(rankMemberService.revocationRankMember(uid));
    }

    /**
     * 批量同步榜单成员索引
     * @param fileUid
     * @return
     */
    @PostMapping("/buildAggDoc")
    String buildRankMemberAggDoc(){
        return rankMemberService.buildRankMemberAggDoc();
    }


    /**
     * 根据各种业务id列表查询对应的对象集合
     * @param fileUid
     * @return
     */
    @PostMapping("/listBusinessObjectByIds")
    R listBusinessObjectByIds(@RequestBody BusinessIdListDto businessIdListDto){
        return R.ok().setData(rankMemberService.listBusinessObjectByIds(businessIdListDto));
    }

    /**
     * 根据id获取榜单成员
     * @param fileUid
     * @return
     */
    @GetMapping("/getRankMemberById")
    R getRankMemberById(@RequestParam("uid") String uid){
        return R.ok().setData(rankMemberService.getRankMemberById(uid));
    }

    /**
     * 获取媒资信息
     */
    @ApiOperation(value = "获取媒资信息", notes = "获取媒资信息", response = String.class)
    @GetMapping(value = "/{uid}")
    public String getInfo(@PathVariable("uid") String uid) {
        RankMemberEntity memberEntity = rankMemberService.getRankMemberById(uid);
        if (memberEntity != null) {
            return ResultUtil.successWithData(memberEntity);
        }
        return ResultUtil.errorWithMessage("获取的媒资信息已被删除！");
    }

    /**
     * 根据用户列表ID  查询各用户发表博客数
     * @param
     * @return
     */
    @GetMapping("/getUsersRankMemberCountByUserIds")
    R getUsersRankMemberCountByUserIds(@RequestParam("userUids") List<String> userUids){
        return R.ok().setData(rankMemberService.getUsersRankMemberCountByUserIds(userUids));
    }
    /**
     * 根据用户id和来源统计榜单成员的数量
     * @param
     * @return
     */
    @GetMapping("/countByUserUidAndSource")
    Integer countRankMemberByUserUidAndSource(@RequestParam("userUid") String userUid, @RequestParam("source") String source){
        return rankMemberService.countRankMemberByUserUidAndSource(userUid, source);
    }

    /**
     * 获取发表的文章数
     * @param createUserUid
     * @param memberUid
     * @param toUserUid
     * @return
     */
    @GetMapping("/getCountByAdminOrUser")
    Integer getRankMemberCountByAdminOrUser(@RequestParam("adminUid") String adminUid, @RequestParam("userUid") String userUid){
        return rankMemberService.getRankMemberCountByAdminOrUser(adminUid, userUid);
    }

    /**
     * 获取文章数
     * @param createUserUid
     * @param memberUid
     * @param toUserUid
     * @return
     */
    @GetMapping("/getRankMemberCount")
    Integer getRankMemberCount(RankMemberDto rankMemberDto){
        return rankMemberService.getRankMemberCount(rankMemberDto);
    }
    /**
     * 根据用户id和文章来源，查询文章的oid和Uid
     * @param createUserUid
     * @param memberUid
     * @param toUserUid
     * @return
     */
    @GetMapping("/listIdByUserUidAndSource")
    R listIdByUserUidAndSource(@RequestParam("userUid") String userUid, @RequestParam("source") String source){
        return R.ok().setData(rankMemberService.listIdByUserUidAndSource(userUid,source));
    }


    @ApiOperation(value = "获取榜单成员列表", notes = "获取榜单成员列表", response = String.class)
    @PostMapping("/list")
    public String list(@Validated({GetList.class}) @RequestBody RankMemberDto rankMemberDto, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        return ResultUtil.successWithData(rankMemberService.getPageList(rankMemberDto));
    }

}
